package cn.jksoft.businessclient.ui.adapter;

import android.content.Context;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.jksoft.businessclient.R;
import cn.jksoft.businessclient.bean.WorkBenchEntity;

/**
 * Created by RogersHuang on 16/4/7.
 */
public class WorkbenchAdapter extends RecyclerView.Adapter<WorkbenchAdapter.ItemViewHolder> {
    private Context context;
    private LayoutInflater inflater;
    private List<WorkBenchEntity.OrderMapBean.DataBean> dataList;
    private View.OnClickListener itemClickListener;
    private View.OnClickListener buttonClickListenner;

    public void setItemClickListener(View.OnClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    public void setButtonClickListenner(View.OnClickListener buttonClickListenner) {
        this.buttonClickListenner = buttonClickListenner;
    }

    public WorkbenchAdapter(Context context , List<WorkBenchEntity.OrderMapBean.DataBean> list) {
        this.context = context;
        this.dataList = list;
        inflater = LayoutInflater.from(context);
    }

    public void addDataList(List<WorkBenchEntity.OrderMapBean.DataBean> list){
        this.dataList.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public ItemViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = inflater.inflate(R.layout.item_workbench, viewGroup, false);
        ItemViewHolder itemViewHolder = new ItemViewHolder(view);
        return itemViewHolder;
    }

    @Override
    public void onBindViewHolder(final ItemViewHolder itemViewHolder, int i) {
        WorkBenchEntity.OrderMapBean.DataBean dataBean = dataList.get(i);
        itemViewHolder.tvCarNumber.setText(dataBean.getCAR_PLATE_NUM());
        itemViewHolder.tvName.setText(dataBean.getCUSTOMER_NAME());
        itemViewHolder.btnComplete.setTag(i);
        itemViewHolder.btnComplete.setOnClickListener(buttonClickListenner);
        if(dataBean.getSERVICE_STATUS() == 0){
            itemViewHolder.btnComplete.setTextColor(context.getResources().getColor(R.color.main_red2));
            itemViewHolder.ivSettlementIcon.setVisibility(View.GONE);
            itemViewHolder.btnComplete.setText("完成");
            itemViewHolder.btnComplete.setBackgroundResource(R.drawable.complete_button_shape_normal);
        }else{
            itemViewHolder.ivSettlementIcon.setVisibility(View.VISIBLE);
            itemViewHolder.btnComplete.setBackgroundResource(R.drawable.settlement_button_shape_normal);
            itemViewHolder.btnComplete.setText("收银");
            itemViewHolder.btnComplete.setTextColor(context.getResources().getColor(R.color.main_gray));
        }
        if(dataBean.getIsHasNewMess()!= null && dataBean.getIsHasNewMess().equals("Y")){
            itemViewHolder.tvNewTag.setVisibility(View.VISIBLE);
        }else{
            itemViewHolder.tvNewTag.setVisibility(View.GONE);
        }
        itemViewHolder.itemView.setTag(i);
        itemViewHolder.itemView.setOnClickListener(itemClickListener);
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public static class ItemViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.iv_settlement_icon)
        ImageView ivSettlementIcon;
        @Bind(R.id.tv_car_number)
        TextView tvCarNumber;
        @Bind(R.id.tv_new_tag)
        TextView tvNewTag;
        @Bind(R.id.tv_name)
        TextView tvName;
        @Bind(R.id.btn_complete)
        AppCompatButton btnComplete;

        public ItemViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
}
